/**
 * Created by wangzhili on 2017/9/21.
 */

$(function () {
    //给下拉选绑定onchange()事件
    var $selector =$('#selector');
    $selector.change(function(){
        setCookie("clusterId",$selector.find("option:selected").val(),1);
        console.log("$selector.find(\"option:selected\").val():"+getCookie("clusterId"));
    });

    $('#myCluster').on('shown.bs.modal',
        function () {
            var url = "/cykmr/cluster/list";
            $.ajax({
                type:'post',
                url: url,
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    initializeClusterSelector(data.result);
                    $('#uploadBtn').attr("data-target","#hadoopModal");
                }
            });
        });
});

function getSelectedCluster() {
    return getCookie("clusterId");
}

function initializeClusterSelector(clusters) {
    //生成下拉选内容
    var $selector =$('#selector');
    $selector.empty();
    var selectedCluster=getSelectedCluster();
    clusters.forEach(function (cluster) {
        var $option = $("<option></option>").val(cluster.clusterId).text(cluster.clusterName).addClass("select-option");
        if(selectedCluster===cluster.clusterId){
            $option.attr("selected",true);
        }
        $selector.append($option);
    });
    setCookie("clusterId",$selector.find("option:selected").val(),1);
}
